package cn.zc.curriculum.service.service.impl;


import cn.tools.core.VO.PageVO;
import cn.tools.core.exception.BizException;
import cn.tools.core.utils.mybatis.PageUtil;
import cn.tools.dozer.config.DozerUtils;
import cn.zc.curriculum.entity.CurriculumClassify;
import cn.zc.curriculum.entity.CurriculumMenu;
import cn.zc.curriculum.entity.Dto.CurriculumMenuDto;
import cn.zc.curriculum.entity.VO.CurriculumMenuVO;
import cn.zc.student.feign.FeignStudentExercisesResult;
import cn.zc.curriculum.service.mapper.CurriculumMenuMapper;
import cn.zc.curriculum.service.service.CurriculumClassifyService;
import cn.zc.curriculum.service.service.CurriculumMenuService;
import cn.zc.curriculum.service.service.MenuAndQuestionService;
import cn.zc.tools.security.securityEntity.SecurityUser;
import cn.zc.tools.security.utils.SecurityUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author keno
 * @since 2022-02-14
 */
@Service
public class CurriculumMenuServiceImpl extends ServiceImpl<CurriculumMenuMapper, CurriculumMenu> implements CurriculumMenuService {

    @Resource
    private CurriculumMenuMapper curriculumMenuMapper;

    @Resource
    private DozerUtils dozerUtils;

    @Resource
    private MenuAndQuestionService menuAndQuestionService;

    @Resource
    private SecurityUtils securityUtils;

    @Resource
    private CurriculumClassifyService curriculumClassifyService;

    @Resource
    private FeignStudentExercisesResult feignStudentExercisesResult;

    /**
     * 查询目录
     * @param pageVO
     * @param classifyId
     * @return
     */
    @Override
    public List<CurriculumMenuDto> findCurriculumMenuByClassify(PageVO pageVO, String classifyId) {
        return curriculumMenuMapper.findCurriculumMenuByClassify(PageUtil.initPage(pageVO),classifyId);

    }


    /**
     * 修改目录
     * @param curriculumMenuDto
     */
    @Override
    public void updateCurriculumMenu(CurriculumMenuDto curriculumMenuDto) {
        CurriculumMenu menu = dozerUtils.map(curriculumMenuDto, CurriculumMenu.class);
        if(!updateById(menu)){
            throw new BizException("修改目录失败");
        }
    }

    /**
     * 删除目录
     * @param menuId
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public void deleteCurriculumMenu(String menuId) {
        if(!removeById(menuId)){
            throw new BizException("删除目录失败");
        };
        menuAndQuestionService.deleteMenuAndQuestion(menuId);

    }

    /**
     * 创建目录
     * @param curriculumMenuDto
     * @return
     */
    @Override
    public CurriculumMenuDto createCurriculumMenu(CurriculumMenuDto curriculumMenuDto) {
        CurriculumMenu menu = dozerUtils.map(curriculumMenuDto, CurriculumMenu.class);
        if(!save(menu)){
            throw new BizException("创建目录失败");
        }
        return dozerUtils.map(menu, CurriculumMenuDto.class);

    }

    /**
     * 查询课程下所有目录
     * @param pageVO
     * @param classifyId
     * @return
     */
    @Override
    public List<CurriculumMenuVO> findAllCurriculumMenuByClassify(PageVO pageVO, String classifyId) {
        CurriculumClassify byId = curriculumClassifyService.getById(classifyId);
        if(byId.getFeeStasus() != null && byId.getFeeStasus() == 1) {
            if (!securityUtils.testedClassify(classifyId)) {
                throw new BizException("你还未购买此课程");
            }
        }
        SecurityUser securityUser = securityUtils.getSecurityUser();
        List<CurriculumMenuVO> allCurriculumMenuByClassify = curriculumMenuMapper.findAllCurriculumMenuByClassify(PageUtil.initPage(pageVO), classifyId);

        //调用feign接口获取成绩
        for (CurriculumMenuVO curriculumMenu:allCurriculumMenuByClassify) {
            curriculumMenu.setExercisesScore(feignStudentExercisesResult.findStudentExercisesResult(curriculumMenu.getId(),securityUser.getId()));
        }

        return allCurriculumMenuByClassify;
    }


}
